<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-row :gutter="48">
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="终端类型">
                  <a-select v-model="clientType" placeholder="请选择" @select="clientTypeClick">
                    <a-select-option :value="item.value" v-for="(item,index) in optionType" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="状态">
                  <a-select v-model="flag" placeholder="请选择" @select="statusClick">
                    <a-select-option :value="item.value" v-for="(item,index) in optionStatus" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-range-picker
              :ranges="{' 今天': [moment(), moment()], '一月内': [moment(), moment().endOf('month')] }"
              @change="onChange"
            />
          </a-col>
          <a-col :md="8" :sm="24">
            <a-input-search placeholder="请输入广告位关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="addAdsClick">新增广告</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :pagination="pagination"
    >
      <span slot="positionName" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="picPath" slot-scope="text">
        <img style="width:25px;height:25px" :src="text" alt="">
      </span>
      <span slot="clientType" slot-scope="text">
        <a-badge :status="text | clientTypeFilter" :text="text | clientFilter" />
      </span>
      <span slot="linkType" slot-scope="text">
        <span>{{ text === 0? '外链' : '内链' }}</span>
      </span>
      <span slot="flag" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
      <span slot="createTime" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
        </template>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { adsList } from '@/api/ads'
import utils from '@/utils/common'
import moment from 'moment'
var timeStamp = Number(new Date())

const statusMap = {
  0: {
    status: 'default',
    text: '下线'
  },
  1: {
    status: 'success',
    text: '上线'
  }
}
const clientMap = {
  0: {
    status: 'success',
    text: '微信'
  },
  1: {
    status: 'success',
    text: '安卓'
  },
  2: {
    status: 'success',
    text: 'IOS'
  },
  3: {
    status: 'success',
    text: '移动端'
  },
  4: {
    status: 'success',
    text: 'PC端'
  }
}

export default {
  name: 'AdsList',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      flag: '',
      optionStatus: [
        { value: '', label: '全部' },
        { value: '1', label: '上线' },
        { value: '0', label: '下线' }
      ],
      clientType: '',
      optionType: [
        { value: '', label: '全部' },
        { value: '1', label: '安卓' },
        { value: '2', label: 'IOS' }
      ],
      // 查询参数
      queryParam: {},
      keyword: '',
      startTime: '',
      endTime: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: '广告位',
          dataIndex: 'positionName',
          scopedSlots: { customRender: 'positionName' }
        },
        {
          title: '广告图',
          dataIndex: 'picPath',
          scopedSlots: { customRender: 'picPath' }
        },
        {
          title: '终端类型',
          dataIndex: 'clientType',
          scopedSlots: { customRender: 'clientType' }
        },
        {
          title: '类别',
          dataIndex: 'linkType',
          scopedSlots: { customRender: 'linkType' }
        },
        {
          title: '状态',
          dataIndex: 'flag',
          scopedSlots: { customRender: 'flag' }
        },
        {
          title: '排序',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return adsList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    },
    clientFilter (type) {
      return clientMap[type].text
    },
    clientTypeFilter (type) {
      return clientMap[type].status
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      clientType: this.clientType,
      flag: this.flag,
      keyword: this.keyword,
      startTime: this.startTime,
      endTime: this.endTime
    }
  },
  methods: {
    moment,
    statusClick (val) { // 状态
      this.queryParam.flag = val
      this.$refs.table.refresh()
    },
    clientTypeClick (val) { // 类型
      this.queryParam.clientType = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    onChange (dates, dateStrings) { // 日期
      this.queryParam.startTime = dateStrings[0]
      this.queryParam.endTime = dateStrings[1]
      this.$refs.table.refresh()
    },
    addAdsClick () { // 添加广告
      this.$router.push('/ads/manage/add')
    },
    handleEdit (record) { // 编辑
      this.$router.push('/ads/manage/edit/' + record.id)
    },
    ApiUpdate (record, index) { // 发布 下架
      var data = {
        id: record.id,
        status: index
      }
      this.IntoUpdate(data)
        .then((res) => this.updateSuccess(res, record.id))
        .catch(err => this.requestFailed(err))
    },
    updateSuccess (res, key) {
      if (res.status === 200 || res.status === '200') {
        this.$notification['success']({
          message: '成功',
          description: res.message,
          duration: 4
        })
        this.$refs.table.refresh()
      } else {
        this.requestFailed(res)
      }
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
